package com.atguigu.educms.controller;


import com.atguigu.commonutils.R;
import com.atguigu.educms.entity.CrmBanner;
import com.atguigu.educms.service.CrmBannerService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 首页banner表 后台banner控制器
 * </p>
 *
 * @author testjava
 * @since 2020-10-27
 */
@Slf4j
@Api("后台banner控制器--底层逻辑使用")
@RestController
@RequestMapping("/educms/banneradmin")
@CrossOrigin
public class BannerAdminController {
    @Autowired
    private CrmBannerService bannerService;

    //1 分页查询banner
    @ApiOperation("分页查询banner")
    @GetMapping("/pageBanner/{pageNum}/{pageSize}")
    public R pageBanner(@PathVariable("pageNum") Integer pageNum,
                        @PathVariable("pageSize") Integer pageSize){

        Page<CrmBanner> pageBanner = new Page<>();
        bannerService.page(pageBanner,null);
        long total = pageBanner.getTotal();
        List<CrmBanner> items = pageBanner.getRecords();
        Map<String,Object> map = new HashMap<>();
        map.put("total",total);
        map.put("items",items);

        return R.ok().data(map);
    }

    //2 添加banner
    @ApiOperation("添加banner")
    @PostMapping("/addBanner")
    public R addBanner(@RequestBody CrmBanner crmBanner){
        boolean flag = bannerService.save(crmBanner);
        if(flag){
            return R.ok();

        }else{
            return R.error();
        }

    }
    //3 修改banner
    @ApiOperation("获取banner")
    @GetMapping("/getBanner/{id}")
    public R getBanner(@PathVariable("id")String id){
        CrmBanner banner = bannerService.getById(id);
        if(banner!=null){

            return R.ok();
        }else{
            return R.error();
        }
    }

    @ApiOperation("修改banner")
    @PutMapping("/updateBanner")
    public R updateBanner(@RequestBody CrmBanner crmBanner){
        boolean flag = bannerService.updateById(crmBanner);
        if(flag){
            return R.ok();
        }else{
            return R.error();
        }
    }


    //4 删除banner
    @ApiOperation("删除banner")
    @DeleteMapping("/deleteBanner/{id}")
    public R deleteBanner(@PathVariable("id") String id){
        bannerService.removeById(id);
        return R.ok();
    }



















}

